package com.cr.admin.business.brand.dao.repository.impl;



import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.admin.business.brand.dao.mapper.BrandMapper;
import com.cr.admin.business.brand.dao.repository.BrandRepository;
import com.cr.admin.business.brand.pojo.entity.Brand;
import com.cr.admin.business.brand.pojo.param.BrandAddParam;
import com.cr.admin.business.brand.pojo.param.BrandUpdateParam;
import com.cr.admin.business.brand.pojo.query.BrandQuery;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


@Repository
public class BrandRepositoryImpl implements BrandRepository {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> selectList(BrandQuery brandQuery) {
        QueryWrapper<Brand> queryWrapper= new QueryWrapper<>();

        if(brandQuery.getName()!=null){
            queryWrapper.like("name",brandQuery.getName());
        }

        return brandMapper.selectList(queryWrapper);
    }

    @Override
    public void insert(BrandAddParam brandAddParam) {
        Brand brand=new Brand();
        BeanUtils.copyProperties(brandAddParam,brand);
        brand.setCreatedTime(new Date());
        brandMapper.insert(brand);
    }
    @Override
    public void updateById(BrandUpdateParam brandUpdateParam) {
        Brand brand=new Brand();
        BeanUtils.copyProperties(brandUpdateParam,brand);
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);
    }

    @Override
    public Brand selectById(Long id) {
        return brandMapper.selectById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        brandMapper.deleteBatchIds(ids);
    }
}
